package qqclient.view;

import qqclient.service.FileClientService;
import qqclient.service.MessageClientService;
import qqclient.service.UserClientService;
import qqclient.utils.Utility;

/**
 * QQ多用户通信系统，包含登入、注册、私聊消息、群发消息、发送文件
 * 离线私聊消息、离线群发消息、离线文件、
 * 获取在线用户列表、退出以及服务器推送消息功能
 *
 * QQ多用户通信系统的客户端
 * 客户端的菜单界面
 */
public class QQView {
    private boolean loop = true;//控制是否显示菜单
    private String key = "";//接收用户的键盘输入
    private UserClientService userClientService = new UserClientService();//该对象用于登入服务器/注册用户
    private MessageClientService messageClientService = new MessageClientService();//该对象用于私聊/群聊
    private FileClientService fileClientService = new FileClientService();//用于传输文件

    public static void main(String[] args) {
        new QQView().mainMenu();
        System.out.println("客户端退出系统。。。");
    }

    //显示主菜单
    private void mainMenu() {
        while (loop) {
            System.out.println("============欢迎登入网络通信系统============");
            System.out.println("\t\t\t 1: 登入系统");
            System.out.println("\t\t\t 2: 注册账号");
            System.out.println("\t\t\t 9: 退出系统");
            System.out.println("请输入你的选择:");
            key = Utility.readString(1);

            switch (key) {
                case "1":
                    System.out.print("请输入你的账号:");
                    String userId = Utility.readString(50);
                    System.out.print("请输入你的密码:");
                    String userPwd = Utility.readString(50);
                    //到服务端去验证该用户是否合法,编写一个类UserClientService[用户登入/注册]
                    if (userClientService.checkUser(userId, userPwd)) {
                        System.out.println("=============欢迎用户(" + userId + ")=============");
                        while (loop) {
                            System.out.println("\n=========网络通信二级菜单(用户" + userId + ")=========");
                            System.out.println("\t\t 1: 显示在线用户列表");
                            System.out.println("\t\t 2: 群发消息");
                            System.out.println("\t\t 3: 私聊消息");
                            System.out.println("\t\t 4: 发送文件");
                            System.out.println("\t\t 9: 退出系统");
                            System.out.println("请输入你的选择:");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    //调用方法，来获取在线用户列表
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("请输入想对大家说的内容:");
                                    String contentAll = Utility.readString(100);
                                    //编写一个方法，将说的内容发送给服务器端
                                    messageClientService.sendMessageToAll(contentAll,userId);
                                    break;
                                case "3":
                                    System.out.print("请输入想进行聊天的用户号:");
                                    String getterId = Utility.readString(50);
                                    System.out.println("请输入想说的内容:");
                                    String content = Utility.readString(100);
                                    //编写一个方法，将说的内容发送给服务器端
                                    messageClientService.sendMessageToOne(content, userId, getterId);
                                    break;
                                case "4":
                                    System.out.print("请输入你想把文件发送给的用户:");
                                    getterId = Utility.readString(50);
                                    System.out.println("请输入发送的文件路径(E:\\idea_java_projects\\myTemp\\t1\\xxx):");
                                    String src = Utility.readString(100);
                                    System.out.println("请输入对方文件接收的路径(E:\\idea_java_projects\\myTemp\\t2\\xxx):");
                                    String dest = Utility.readString(100);
                                    fileClientService.sendFileToOne(src,dest,userId,getterId);
                                    break;
                                case "9":
                                    //调用方法，给服务器发送一个退出系统的Message
                                    userClientService.logout();
                                    loop = false;
                                    break;
                            }
                        }
                    } else {
                        System.out.println("登入失败");
                    }
                    break;
                case "2":
                    register();
                    break;
                case "9":
                    loop = false;
                    break;
            }
        }
    }
    // 用户注册
    private void register() {
        System.out.print("请输入要注册的账号:");
        String userId = Utility.readString(50);
        System.out.print("请输入要注册的密码:");
        String userPwd = Utility.readString(50);
        if (userClientService.registerUser(userId, userPwd)) {
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败！");
        }
    }

}
